using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CUser : CBaseDynamic, IComparable<CUser>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CUser(CUser original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _userSchemaId = original.UserSchemaId;
            _userNodeGuid = original.UserNodeGuid;
            _userTypeId = original.UserTypeId;
            _userIsActive = original.UserIsActive;
            _userName = original.UserName;
            _userTokenTemp = original.UserTokenTemp;
            _userTokenEncr = original.UserTokenEncr;
            _userTokenExpires = original.UserTokenExpires;
            _userTokenHasExpired = original.UserTokenHasExpired;
            _userCreated = original.UserCreated;
            _userUpdated = original.UserUpdated;
        }

        //Protected (Datareader/Dataset)
        protected CUser(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CUser(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _userId = long.MinValue;
            _userSchemaId = int.MinValue;
            _userNodeGuid = Guid.Empty;
            _userTypeId = int.MinValue;
            _userIsActive = false;
            _userName = string.Empty;
            _userTokenTemp = string.Empty;
            _userTokenEncr = new byte[]{};
            _userTokenExpires = DateTime.MinValue;
            _userTokenHasExpired = false;
            _userCreated = DateTime.MinValue;
            _userUpdated = DateTime.MinValue;
        }
        #endregion

        #region Members
        protected long _userId;
        protected int _userSchemaId;
        protected Guid _userNodeGuid;
        protected int _userTypeId;
        protected bool _userIsActive;
        protected string _userName;
        protected string _userTokenTemp;
        protected byte[] _userTokenEncr;
        protected DateTime _userTokenExpires;
        protected bool _userTokenHasExpired;
        protected DateTime _userCreated;
        protected DateTime _userUpdated;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public long UserId    {   get { return _userId;   } }

        //Table Columns (Read/Write)
        public int UserSchemaId    {   get { return _userSchemaId; } set { _userSchemaId = value; }    }
        public Guid UserNodeGuid    {   get { return _userNodeGuid; } set { _userNodeGuid = value; }    }
        public int UserTypeId    {   get { return _userTypeId; } set { _userTypeId = value; }    }
        public bool UserIsActive    {   get { return _userIsActive; } set { _userIsActive = value; }    }
        public string UserName    {   get { return _userName; } set { _userName = value; }    }
        public string UserTokenTemp    {   get { return _userTokenTemp; } set { _userTokenTemp = value; }    }
        public byte[] UserTokenEncr    {   get { return _userTokenEncr; } set { _userTokenEncr = value; }    }
        public DateTime UserTokenExpires    {   get { return _userTokenExpires; } set { _userTokenExpires = value; }    }
        public bool UserTokenHasExpired    {   get { return _userTokenHasExpired; } set { _userTokenHasExpired = value; }    }
        public DateTime UserCreated    {   get { return _userCreated; } set { _userCreated = value; }    }
        public DateTime UserUpdated    {   get { return _userUpdated; } set { _userUpdated = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Users";
        public const string VIEW_NAME       = "";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "UserId DESC";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CUser other) {   return this.UserId.CompareTo(other.UserId) *-1;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "UserId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _userId; }
              set { _userId = Convert.ToInt64(value); }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CUser(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CUser(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CUserList();               }
        protected override IList MakeList(int capacity)     {   return new CUserList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _userId = CAdoData.GetLong(dr, "UserId");
            _userSchemaId = CAdoData.GetInt(dr, "UserSchemaId");
            _userNodeGuid = CAdoData.GetGuid(dr, "UserNodeGuid");
            _userTypeId = CAdoData.GetInt(dr, "UserTypeId");
            _userIsActive = CAdoData.GetBool(dr, "UserIsActive");
            _userName = CAdoData.GetStr(dr, "UserName");
            _userTokenTemp = CAdoData.GetStr(dr, "UserTokenTemp");
            _userTokenEncr = CAdoData.GetBytes(dr, "UserTokenEncr");
            _userTokenExpires = CAdoData.GetDate(dr, "UserTokenExpires");
            _userTokenHasExpired = CAdoData.GetBool(dr, "UserTokenHasExpired");
            _userCreated = CAdoData.GetDate(dr, "UserCreated");
            _userUpdated = CAdoData.GetDate(dr, "UserUpdated");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _userId = CAdoData.GetLong(dr, "UserId");
            _userSchemaId = CAdoData.GetInt(dr, "UserSchemaId");
            _userNodeGuid = CAdoData.GetGuid(dr, "UserNodeGuid");
            _userTypeId = CAdoData.GetInt(dr, "UserTypeId");
            _userIsActive = CAdoData.GetBool(dr, "UserIsActive");
            _userName = CAdoData.GetStr(dr, "UserName");
            _userTokenTemp = CAdoData.GetStr(dr, "UserTokenTemp");
            _userTokenEncr = CAdoData.GetBytes(dr, "UserTokenEncr");
            _userTokenExpires = CAdoData.GetDate(dr, "UserTokenExpires");
            _userTokenHasExpired = CAdoData.GetBool(dr, "UserTokenHasExpired");
            _userCreated = CAdoData.GetDate(dr, "UserCreated");
            _userUpdated = CAdoData.GetDate(dr, "UserUpdated");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("UserId", CAdoData.NullVal(_userId));
            data.Add("UserSchemaId", CAdoData.NullVal(_userSchemaId));
            data.Add("UserNodeGuid", CAdoData.NullVal(_userNodeGuid));
            data.Add("UserTypeId", CAdoData.NullVal(_userTypeId));
            data.Add("UserIsActive", CAdoData.NullVal(_userIsActive));
            data.Add("UserName", CAdoData.NullVal(_userName));
            data.Add("UserTokenTemp", CAdoData.NullVal(_userTokenTemp));
            data.Add("UserTokenEncr", CAdoData.NullVal(_userTokenEncr));
            data.Add("UserTokenExpires", CAdoData.NullVal(_userTokenExpires));
            data.Add("UserTokenHasExpired", CAdoData.NullVal(_userTokenHasExpired));
            data.Add("UserCreated", CAdoData.NullVal(_userCreated));
            data.Add("UserUpdated", CAdoData.NullVal(_userUpdated));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CUserList SelectAll()                                                          {    return (CUserList)base.SelectAll();                              }
        public new CUserList SelectAll(string orderBy)                                            {    return (CUserList)base.SelectAll(orderBy);                       }
        public new CUserList SelectWhere(string colName, ESign sign, object colValue)             {    return (CUserList)base.SelectWhere(colName, sign, colValue);     }
        public new CUserList SelectWhere(CCriteria where)                                         {    return (CUserList)base.SelectWhere(where);                       }
        public new CUserList SelectWhere(CCriteriaList where)                                     {    return (CUserList)base.SelectWhere(where);                       }
        public new CUserList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CUserList)base.SelectWhere(where, tableOrJoin);          }
        public new CUserList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CUserList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CUserList SelectWhere(string unsafeWhereClause)                                {    return (CUserList)base.SelectWhere(unsafeWhereClause);           }
        public     CUserList SelectById(long userId)                              {    return (CUserList)base.SelectById(userId);       }
        public     CUserList SelectByIds(List<long> ids)                        {    return (CUserList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CUserList SelectAll(  CPagingInfo pi)                                              {    return (CUserList)base.SelectAll(  pi);                              }
        public new CUserList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CUserList)base.SelectWhere(pi, name, sign, value);           }
        public new CUserList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CUserList)base.SelectWhere(pi, criteria);                    }
        public new CUserList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CUserList)base.SelectWhere(pi, criteria);                    }
        public new CUserList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CUserList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CUserList SelectByIds(CPagingInfo pi, List<long> ids)            {    return (CUserList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CUserList SelectAll(                                                                                           IDbTransaction tx)  {    return (CUserList)base.SelectAll(tx);                                                     }
        public new CUserList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CUserList)base.SelectAll(orderBy,                                         tx);    }
        public new CUserList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CUserList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CUserList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CUserList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CUserList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CUserList)base.SelectWhere(               criteria,                       tx);    }
        public new CUserList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CUserList)base.SelectWhere(               criteria,                       tx);    }
        public new CUserList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CUserList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CUserList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CUserList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CUserList SelectById(long userId,                                                              IDbTransaction tx)  {    return (CUserList)base.SelectById(userId,                         tx);    }
        public     CUserList SelectByIds(List<long> ids,                                                        IDbTransaction tx)  {    return (CUserList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CUserList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CUserList)base.MakeList(storedProcName,             txOrNull);  }
        public new CUserList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CUserList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CUserList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CUserList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CUserList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CUserList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CUserList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CUserList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CUserList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CUserList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CUserList MakeList(DataSet              ds) { return (CUserList)base.MakeList(ds);        }
        protected new CUserList MakeList(DataTable            dt) { return (CUserList)base.MakeList(dt);        }
        protected new CUserList MakeList(DataRowCollection  rows) { return (CUserList)base.MakeList(rows);      }
        protected new CUserList MakeList(IDataReader          dr) { return (CUserList)base.MakeList(dr);        }
        protected new CUserList MakeList(object           drOrDs) { return (CUserList)base.MakeList(drOrDs);    }        
        protected new CUserList MakeList(byte[]             gzip) { return (CUserList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CUserList SelectBySchemaId(int userSchemaId)    {    return SelectWhere(new CCriteriaList("UserSchemaId", userSchemaId));    }
        public CUserList SelectByNodeGuid(long UserNodeGuid)    {    return SelectWhere(new CCriteriaList("UserNodeGuid", UserNodeGuid));    }
        public CUserList SelectByTypeId(int userTypeId)    {    return SelectWhere(new CCriteriaList("UserTypeId", userTypeId));    }
        public CUserList SelectByIsActive(bool userIsActive)    {    return SelectWhere(new CCriteriaList("UserIsActive", userIsActive));    }
        public CUserList SelectByName(string userName)    {    return SelectWhere(new CCriteriaList("UserName", userName));    }
        public CUserList SelectByTokenHasExpired(bool userTokenHasExpired)    {    return SelectWhere(new CCriteriaList("UserTokenHasExpired", userTokenHasExpired));    }

        //Paged
        public CUserList SelectBySchemaId(CPagingInfo pi, int userSchemaId)    {    return SelectWhere(pi, new CCriteriaList("UserSchemaId", userSchemaId));    }
        public CUserList SelectByNodeGuid(CPagingInfo pi, long UserNodeGuid)    {    return SelectWhere(pi, new CCriteriaList("UserNodeGuid", UserNodeGuid));    }
        public CUserList SelectByTypeId(CPagingInfo pi, int userTypeId)    {    return SelectWhere(pi, new CCriteriaList("UserTypeId", userTypeId));    }
        public CUserList SelectByIsActive(CPagingInfo pi, bool userIsActive)    {    return SelectWhere(pi, new CCriteriaList("UserIsActive", userIsActive));    }
        public CUserList SelectByName(CPagingInfo pi, string userName)    {    return SelectWhere(pi, new CCriteriaList("UserName", userName));    }
        public CUserList SelectByTokenHasExpired(CPagingInfo pi, bool userTokenHasExpired)    {    return SelectWhere(pi, new CCriteriaList("UserTokenHasExpired", userTokenHasExpired));    }

        //Count
        public int SelectCountBySchemaId(int userSchemaId)   {   return SelectCount(new CCriteriaList("UserSchemaId", userSchemaId));     }
        public int SelectCountByNodeGuid(long UserNodeGuid)   {   return SelectCount(new CCriteriaList("UserNodeGuid", UserNodeGuid));     }
        public int SelectCountByTypeId(int userTypeId)   {   return SelectCount(new CCriteriaList("UserTypeId", userTypeId));     }
        public int SelectCountByIsActive(bool userIsActive)   {   return SelectCount(new CCriteriaList("UserIsActive", userIsActive));     }
        public int SelectCountByName(string userName)   {   return SelectCount(new CCriteriaList("UserName", userName));     }
        public int SelectCountByTokenHasExpired(bool userTokenHasExpired)   {   return SelectCount(new CCriteriaList("UserTokenHasExpired", userTokenHasExpired));     }

        //Transactional
        public CUserList SelectBySchemaId(int userSchemaId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("UserSchemaId", userSchemaId), tx);    }
        public CUserList SelectByNodeGuid(long UserNodeGuid, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("UserNodeGuid", UserNodeGuid), tx);    }
        public CUserList SelectByTypeId(int userTypeId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("UserTypeId", userTypeId), tx);    }
        public CUserList SelectByIsActive(bool userIsActive, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("UserIsActive", userIsActive), tx);    }
        public CUserList SelectByName(string userName, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("UserName", userName), tx);    }
        public CUserList SelectByTokenHasExpired(bool userTokenHasExpired, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("UserTokenHasExpired", userTokenHasExpired), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "UserId", this.UserId);
            Store(w, "UserSchemaId", this.UserSchemaId);
            Store(w, "UserNodeGuid", this.UserNodeGuid);
            Store(w, "UserTypeId", this.UserTypeId);
            Store(w, "UserIsActive", this.UserIsActive);
            Store(w, "UserName", this.UserName);
            Store(w, "UserTokenTemp", this.UserTokenTemp);
            Store(w, "UserTokenEncr", this.UserTokenEncr);
            Store(w, "UserTokenExpires", this.UserTokenExpires);
            Store(w, "UserTokenHasExpired", this.UserTokenHasExpired);
            Store(w, "UserCreated", this.UserCreated);
            Store(w, "UserUpdated", this.UserUpdated);
        }
        #endregion

    }
}
